#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from enum import Enum
from common.env_common import get_install_head_path


class SaphanaServiceConst:
    LIST_APPLICATION_RESOURCE = "ListApplicationResource"
    CHECK_APPLICATION = "CheckApplication"


class SaphanaConst:
    # 多租户模式下系统数据库名称
    SYSTEM_DB = "SYSTEMDB"
    # GetSystemInstanceList中有效的host信息里的参数数量，以,符号分隔的
    INSTANCE_INFO_ELE_SIZE = 7
    # 数据库类型: 租户数据库
    TENANT_DATABASE = "TenantDatabase"
    # 数据库类型: 系统数据库
    SYSTEM_DATABASE = "SystemDatabase"


class SaphanaModel(str, Enum):
    # 多租户模式
    MULTI_TENANT_MODE = "multidb"


class SaphanaClusterStatus(str, Enum):
    # 集群在线
    ONLINE = "true"
    OFFLINE = "false"


class SaphanaErrorCode(int, Enum):
    # 不支持选择当前认证模式
    NOT_SUPPORT_LOGIN_MODEL = 1577213495
    # 认证信息错误
    ERROR_AUTH = 1577209942
    # 注册租户数据库时，需要输入系统数据库用户名和密码
    LACK_SYSTEM_INFO = 1577213493
    # 登录系统数据库失败
    LOGIN_SYSTEM_DB_ERROR = 1577213494
    # 类型和所选主机不匹配
    TYPE_AND_AGENTS_NOT_MATCH = 1577213492
    # 自定义参数中的systemId在生产环境上不存在。
    SYSTEM_ID_NOT_EXIST = 1577213473
    # 数据库在生产环境上分布的节点（[{0}]）与资源注册时选的节点（[{1}]）不一致。
    DB_NODES_NOT_FULL_MATCH = 1577213474
    # 产环境异常。
    DB_ENV_ERROR = 1577213475
    # 登录数据库失败。
    ACCESS_DB_ERROR = 1577213476
    # 不支持选择的部署类型。
    TENANT_MODEL_NOT_SUPPORT = 1577213490
    # 数据库在生产环境上不存在。
    DB_NOT_EXIST = 1577213477
    # 部署类型不匹配
    DEPLOY_TYPE_ERROR = 1577213484
    # 集群不在线
    CLUSTER_NOT_ONLINE = 1577213478
    # 参数错误
    ERROR_PARAM = 1677929218
    # 不允许更换保护的数据库
    NOT_ALLOW_CHANGE_PROTECT_DB = 1577213481
    # 不能修改数据库为新的数据库
    NOT_ALLOW_CHANGE_DB_TO_NEW_DB = 1577213482
    # 系统异常
    SYSTEM_ERROR = 1677929221
    # 数据库的服务（[{0}]）未运行。
    NOT_ALL_DB_SERVICE_RUNNING = 1577213479
    # 增量转全量
    INC_TO_FULL = 0x5E02502D
    # 生产环境上日志不连续。
    LOG_INCONSISTENT = 1577213480
    # 生产环境没有新的日志副本。
    NO_NEW_LOG_BACKUP = 1577213485
    # 生产环境上新生成的日志副本文件（[{0}]）不存在。
    LOG_BACKUP_FILE_NOT_EXIST = 1577213487
    # 生产环境上配置的日志备份目录（{0}）不存在。
    LOG_BACKUP_PATH_NOT_EXIST = 1577213486
    # 备份日志备份文件失败
    BACKUP_LOG_FILE_FAIL = 1577213488
    # 计算日志备份的开始和结束时间失败。
    CALCULATE_LOG_TIME_FAIL = 1577213489
    # 备份副本的数据库版本({0})与目标位置数据库版本({1})不匹配。
    VERSION_DISMATCH = 1577209921
    # 副本的拓扑结构与目标位置拓扑结构不匹配。
    TOPOLOGY_DISMATCH = 1577209926
    # 备份副本的数据库租户模式（{0}）与目标位置数据库租户模式（{1}）不一致
    MULTI_MODE_DISMATCH = 1577209927
    # 目标数据库未运行
    DB_NOT_RUNNING = 1577213496
    # 获取日志备份目录失败
    GET_LOG_BACKUP_PATH_FAIL = 1577213497
    # 数据库未关闭
    DB_NOT_CLOSE = 1577209950
    # system id不一致
    SYSTEM_ID_NOT_EQUAL = 1577213498
    # 执行备份/恢复命令失败
    EXEC_BACKUP_RECOVER_CMD_FAIL = 1577209989
    # 外置代理主机内存不足
    AGENT_INSUFFICIENT_MEMORY = 1677873256
    # 系统数据库与租户数据库不能互相恢复
    SYSTEM_TENANT_NOT_RECOVER_EACH_OTHER = 1577213499
    # 系统数据库恢复时，发现有正在运行的租户数据库恢复任务
    HANA_RESTORE_BUSY = 1577210038
    # 租户数据库恢复时，发现有正在运行的系统数据库恢复任务
    HANA_RESTORE_SYSTEMDB_BLOCK_TENANTDB = 1577213516
    # 返回框架报错
    ERROR_FOR_FRAMEWORK = 200
    # 返回框架正确
    SUCCESS_FOR_FRAMEWORK = 0
    # 修改数据库配置失败
    DB_CONFIG_FAILED = 1577213511
    # 获取版本号失败
    DB_VERSION_FAILED = 1577213512
    # 获取uuid是失败
    DB_UUID_FAILED = 1577213513
    # 路径不存在
    PATH_NOT_EXIST = 1577213514
    # 安装backint失败
    INSTALL_BACKINT_FAILED = 1577213515
    # 日志备份路径不在共享目录下。
    LOG_BACKUP_PATH_NOT_SHARED = 1577213590


class SaphanaJsonConstant:
    ORIGIN_PROTECTED_RESOURCE = "originProtectedResource"
    RESOURCE_LIST = "resourceList"
    DEPLOY_TYPE = "deployType"
    DB_DEPLOY_TYPE = "sapHanaDbDeployType"
    STATUS = "status"
    AUTH_CUSTOM_PARAMS = "authCustomParams"
    APP_ENV = "appEnv"
    HOSTS = "hosts"
    IS_NEED_CLOSE_DB = "isNeedCloseDb"
    DEPENDENCIES = "dependencies"
    CLUSTER_UUID = "clusterUuid"
    CUSTOM_PARAMS = "customParams"
    PROTECT_DATABASE = "database"
    SYSTEM_DB_USER = "systemDbUser"
    SYSTEM_DB_PASSWORD = "systemDbPassword"
    SYSTEM_ID = "systemId"
    SYSTEM_DB_PORT = "systemDbPort"
    EXTEND_INFO = "extendInfo"
    CLUSTER_TYPE = "clusterType"
    JOB = "job"
    JOB_PARAM = "jobParam"
    BACKUP_TYPE = "backupType"
    PROTECT_OBJECT = "protectObject"
    SUB_TYPE = "subType"
    PROTECT_ENV = "protectEnv"
    PROTECTED_ENV = "protectedEnv"
    ENDPOINT = "endpoint"
    AUTH = "auth"
    AUTH_KEY = "authKey"
    AUTH_TYPE = "authType"
    AUTH_PWD = "authPwd"
    INSTANCE_PORT = "instancePort"
    NODES = "nodes"
    REPOSITORIES = "repositories"
    REPOSITORY_TYPE = "repositoryType"
    PATH = "path"
    BACK_JOB_RESULT = "backupJobResult"
    COPY = "copy"
    COPY_ID = "copyId"
    ARCHIVE_COPY_ID = "copy_id"
    TIMESTAMP = "timestamp"
    BACKUP_TIME = "backupTime"
    BACKUP_LIMIT = "BackupLimit"
    NAME = "name"
    COPIES = "copies"
    TARGET_OBJECT = "targetObject"
    RESTORE_JOB = "restoreType"
    RESTORE_JOB_RESULT = "restoreJobResult"
    PROGRESS = "progress"
    TASK_ID = "taskId"
    SUB_TASK_ID = "subTaskId"
    TASK_STATUS = "taskStatus"
    DATA_SIZE = "dataSize"
    SPEED = "speed"
    LOG_DETAIL = "logDetail"
    APPLICATION = "application"
    APPLICATIONS = "applications_0"
    JOB_TYPE = "jobType"
    LIVE_MOUNT = "liveMount"
    TARGET_ENV = "targetEnv"
    ROLE = "role"
    ADVANCE_PARAMS = "advanceParams"
    SUB_JOB = "subJob"
    JOB_NAME = "jobName"
    RESTORE_TIMESTAMP = "restoreTimestamp"
    TYPE = "type"
    TYPES = "types"
    REMOTE_HOST = "remoteHost"
    REMOTE_PATH = "remotePath"
    MOUNT_JOB_ID = "mountJobId"
    LOG_INFO = "logInfo"
    LOG_INFO_PARAM = "logInfoParam"
    LOG_LEVEL = "logLevel"
    BACKUP_TASK_SLA = "backupTask_sla"
    POLICY_LIST = "policy_list"
    EXT_PARAMETERS = "ext_parameters"
    CHANNEL_NUMBER = "channel_number"
    NEW_DATABASE_NAME = "newDatabaseName"
    APPLICATION_VERSION = "applicationVersion"
    DATA_DIR = "dataDir"
    AVERAGE_SPEED = "averageSpeed"
    LOG_FLAG = "logFlag"
    LOG_FLAG_START_TIME = "logFlagStartTime"
    RESTORE_COPY_ID = "restoreCopyId"
    SERVICE_NAME = "serviceName"
    ID = "id"
    BACKUP_HOST_SN = "backupHostSN"
    FIRST_FULL_BACKUP_TIME = "firstFullBackupTime"
    NEXT_CAUSE_PARAM = "next_cause_param"
    HOST_AGENT = "hostAgent"
    BACKUP_ID = "backupId"
    BACKUP_PATH = "backupPath"
    SAPHANA_VERSION = "saphanaVersion"
    MULTI_TENANT_SYSTEM = "multiTenantSystem"
    IS_LOCAL = "isLocal"
    AGENT_IP_LIST = "agentIpList"
    TARGET_LOCATION = "targetLocation"
    KEY_INFO = "hdbUserStoreKey"
    SYSTEMDB_AUTH_TYPE = "systemdb_auth_type"
    PROTECTDB_AUTH_TYPE = "protectdb_auth_type"
    LOG_BACKUP_EXT_INFO = "logBackupExtInfo"
    LOG_BACKUP_PATH = "logBackupPath"
    LOG_BACKUP_INTERVAL = "logBackupInterval"
    LOG_BACKUP_INTERVAL_UNIT = "logBackupIntervalUnit"
    DB_USER = "dbUser"
    DB_PASSWORD = "dbPassword"
    INSTANCE_AUTH = "instanceAuth"
    DATA_TYPE = "sapHanaDbType"


class SaphanaPath:
    HANA_LIVE_MOUNT_PATH = "/mnt/databackup/"
    HANA_OPT_PATH = f"{get_install_head_path()}/DataBackup/"
    BACKINT_PATH = "/opt/DataBackup/ProtectClient/Plugins/GeneralDBPlugin/bin/backint"


class SaphanaSubjobName:
    RESTORE = "restore"
    PREPARE = "prepare"
    BACKUP = "backup"
    POST = "post"


class SaphanaRentryCount:
    REPORT_PROGRESS_RENTRY_COUNT = 10


class SaphanaReportInterval:
    REPORT_PROGRESS_INTERVAL = 10


class SaphanaJobType:
    RESTORE_PREDECESSOR = 1
    RESTORE_RESTORE = 2
    RESTORE_POST = 3


class SaphanaProgressPhase:
    RUNNING = 5
    WILL_END = 99
    END = 100


class SaphanaStateName:
    SUCCESS = "successful"
    FAIL = "failed"
    RUNNING = "running"
    CANCELED = "canceled"
    CANCELING = "cancel pending"
    ALREADY_CANCELED = "does not contain"


class SaphanaTaskLabel(str, Enum):
    # "子任务（{0}）备份成功，共备份数据量：{1}。"
    BACKUP_SUCCESS_LABEL = "hana_backup_success_label"
    # "数据保护代理主机（{0}）正在执行备份子任务（{1}），已备份数据量：（{2}）。"
    BACKUP_RUNNING_LABEL = "hana_backup_label"
    # "子任务（{0}）备份失败。"
    BACKUP_FAIL_LABEL = "plugin_backup_subjob_fail_label"
    # 执行前置任务失败
    PREREQUISITE_FAIL_LABEL = "plugin_execute_prerequisit_task_fail_label"
    # "生成子任务失败。"
    GENERATE_SUBJOB_FAIL_LABEL = "plugin_generate_subjob_fail_label"
    # 数据保护代理主机已经准备好恢复目录：（{0}），请用HANA studio完成恢复之后，再点击上报按钮，上报恢复结果
    RESTORE_SYSTEMDB_PATH_LABEL = "plugin_hana_systemdb_restore_label"
    # "子任务（{0}）恢复失败。"
    RESTORE_SUBJOB_FAIL_LABEL = "plugin_restore_subjob_fail_label"
    # 子任务（{0}）执行成功。
    RESTORE_SUBJOB_SUCCESS_LABEL = "plugin_task_subjob_success_label"
    # "开始执行恢复子任务（{0}）。"
    RESTORE_START_COPY_SUBJOB = "hana_start_restore_copy_subjob_label"
    # 开始执行恢复挂载子任务（{0}）。
    RESTORE_START_MOUNT_SUBJOB = "hana_plugin_start_restore_prepare_subjob_label"
    # 客户端 {1} 读取备份管道数据超时，建议停止备份任务。
    HANA_BACKUP_PIPE_TIMEOUT = "hana_backup_pipe_timeout"
    # 客户端 {1} 恢复任务超时，请参考数据备份特性指南(适用于 {2} )重新恢复。
    HANA_RESCOVERY_PROGRESS_TIMEOUT = "hana_recovery_progress_timeout"


class SaphanaMetadataKey(str, Enum):
    BACKUP_ID = "backupId"
    DB_INFO = "databaseInfo"  # 数据库分布的节点，以及服务信息
    BACKUP_HOSTNAME = "backupHostname"  # 此次备份的节点名称
    SAPHANA_VERSION = "saphanaVersion"  # 备份时集群版本
    MULTI_TENANT_SYSTEM = "multiTenantSystem"  # 是否为多租户系统。True：是
    MASTER_SYSTEMDB_HOSTNAME = "masterSystemdbHost"  # 主系统数据库所在的节点名称
    BACKUP_LOG_POSTION = "backupPostion"  # 数据备份时的log postion
    LOG_DIR_NAME = "logDirName"
    ASSOCIATE_COPYIES = "associatedCopies"
    LOG_BEGIN_TIME = "beginTime"
    LOG_END_TIME = "endTime"
    BACKUP_TIME = "backupTime"
    BACKUP_PATH = "backupPath"
    COPY_ID = "copy_id"
    SYSTEM_ID = "system_id"


class SaphanaDbActionType:
    QUERY_CMD = 0  # 查询动作
    PROTECT_CMD = 1  # 备份恢复动作


class SaphanaRpcParamKey:
    APPLICATION = "application"
    TYPES = "types"
    COPY_ID = "copyId"
    JOB_ID = "jobId"


class SaphanaLogBackupStage:
    BACKUP_NOT_START = 0  # 日志未开始备份
    BACKUP_RUNNING = 1  # 日志备份中
    BACKUP_SUCCESS = 2  # 日志备份成功
    BACKUP_FAILED = 3  # 日志备份失败


class SaphanaReportStatus:
    SUCCESS = "SUCCESS"
    FAIL = "FAIL"


class SaphanaFileBackupRet:
    SUCCESS = 1
    RUNNING = 2
    FAILED = 3


class LogFileCopyMax:
    MAX_LEN_EACH_BACKUP = 1000


class CommitTimeType:
    START_TIME = 1,
    END_TIME = 2


class ListAppQueryConstant(str, Enum):
    SYSTEM_DB = 'systemDb'
    NOT_SYSTEM_DB = 'notSystemDb'


class SapnanBackupType:
    FULL_BACKUP = "complete data backup"
    INCREMENTAL_BACKUP = "incremental data backup"
    DIFFERENTIAL_BACKUP = "differential data backup"
    LOG_BACKUP = "log backup"
